﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WellDone
{
    public partial class NewPinDialog : ChildWindow
    {
        public NewPinDialog()
        {
            InitializeComponent();
        }

        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // Ensure new pin and confirmed pin are the same
            if (pbNew.Password != pbConfirm.Password)
            {
                MessageBox.Show("New PIN does not match confirmed PIN.");
                return;
            }
            // All is well, update the new pin
            Globals.selectedLogin.col_PIN = pbNew.Password;
            Globals.selectedLogin.bFilterPIN = true;
            if (await Globals.db.UpdateLoginObject(Globals.selectedLogin) == 1)
                MessageBox.Show("PIN updated.");
            else
                MessageBox.Show("Error updating PIN.");

            this.DialogResult = true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void pbNew_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || (e.Key == Key.Tab))
                e.Handled = false;
            else e.Handled = true;
        }

        private void pbConfirm_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || (e.Key == Key.Tab))
                e.Handled = false;
            else e.Handled = true;
        }
    }
}

